package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.entity.Menu;
import com.hotel.entity.Role;
import com.hotel.entity.RoleMenu;
import com.hotel.enums.RoleStatusEnum;
import com.hotel.exception.BusinessException;
import com.hotel.mapper.MenuMapper;
import com.hotel.mapper.RoleMapper;
import com.hotel.mapper.RoleMenuMapper;
import com.hotel.service.RoleService;
import com.hotel.vo.RoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author XiaoGe
 * @since 2022-05-28
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public void authority(Long id, Long[] mids) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            throw new BusinessException("该角色不存在");
        }
        //先删除原来的权限
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId,id);
        roleMenuMapper.delete(wrapper);
        //增加现在分配的角色
        if(mids.length>0){
            for (Long mid : mids) {
                Menu menu = menuMapper.selectById(mid);
                if(menu==null){
                    throw new BusinessException("menuId="+mid+",菜单权限不存在");
                }else {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(id);
                    roleMenu.setMenuId(mid);
                    roleMenuMapper.insert(roleMenu);
                }
            }
        }
    }

    @Override
    public List<Long> findMenuIdsByRoleId(Long id) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            throw new BusinessException("该角色已不存在");
        }
        List<Long> ids=new ArrayList<>();
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId,id);
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(roleMenus)){
            for (RoleMenu roleMenu : roleMenus) {
                ids.add(roleMenu.getMenuId());
            }
        }
        return ids;
    }

    @Override
    public void updateStatus(Long id, Boolean status) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            throw new BusinessException("角色不存在");
        }
        Role t = new Role();
        t.setId(id);
        t.setStatus(status? RoleStatusEnum.DISABLE.getStatusCode():
                RoleStatusEnum.AVAILABLE.getStatusCode());
        roleMapper.updateById(t);
    }

    @Override
    public RoleVO edit(Long id) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            throw new BusinessException("编辑的角色不存在");
        }
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role,roleVO);
        System.out.println(roleVO);
        System.out.println("--------------------------");
        return roleVO;
    }

    @Override
    public void updateRole(RoleVO roleVO) {
        Role dbRole = roleMapper.selectById(roleVO.getId());
        if(dbRole==null){
            throw new BusinessException("要更新的角色不存在");
        }
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleName,roleVO.getRoleName());
        List<Role> roles = roleMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(roles)){
            Role role = roles.get(0);
            if(!role.getId().equals(role.getId())){
                throw new BusinessException("该角色名已被占用");
            }
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleVO,role);
        role.setModifiedTime(new Date());
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long id) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            throw new BusinessException("要删除的角色不存在");
        }
        roleMapper.deleteById(id);
        //删除对应的[角色-菜单]记录
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId,id);
        roleMenuMapper.delete(wrapper);
    }


}
